package com.kaifamiao.annotation.mapper;

import com.kaifamiao.annotation.entity.Person;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PersonMapper {

    @SelectKey( statement = "SELECT ifnull( max(id) , 0 ) + 1 FROM t_persons" ,
                resultType = Integer.class ,
                keyProperty = "id",
                before = true ,
                databaseId = "mysql" )
    @SelectKey( statement = "SELECT nvl( max(id) , 0 ) + 1 FROM t_persons" ,
                resultType = Integer.class ,
                keyProperty = "id",
                before = true ,
                databaseId = "oracle" )
    @Insert( "INSERT INTO t_persons " +
             "( id , name , gender , birthdate , married ) " +
             "VALUES " +
             "( #{id} , #{name} , #{gender} , #{birthdate,jdbcType=DATE} , #{maritalStatus})" )
    int save( Person p ) ;

    @Update( "UPDATE t_persons " +
             " SET name = #{name} , gender = #{gender} , birthdate = #{birthdate,jdbcType=DATE} , married = #{maritalStatus} " +
             " WHERE id = #{id}" )
    int update( Person p ) ;

    @Delete( "DELETE FROM t_persons WHERE id = #{id}" )
    int delete( Integer id ) ;

    @Results( value = {
            @Result( column = "id" , jdbcType = JdbcType.INTEGER, property = "id" ) ,
            @Result( column = "name" , jdbcType = JdbcType.VARCHAR, property = "name" ) ,
            @Result( column = "gender" , jdbcType = JdbcType.VARCHAR, property = "gender" ) ,
            @Result( column = "birthdate" , jdbcType = JdbcType.DATE, property = "birthdate" ) ,
            @Result( column = "married" , property = "maritalStatus" )
    })
    @Select( "SELECT id , name , gender , birthdate , married " +
             "FROM t_persons " +
             "WHERE id = #{id}" )
    Person find( Integer id ) ;

    @Results( value = {
            @Result( column = "id" , jdbcType = JdbcType.INTEGER, property = "id" ) ,
            @Result( column = "name" , jdbcType = JdbcType.VARCHAR, property = "name" ) ,
            @Result( column = "gender" , jdbcType = JdbcType.VARCHAR, property = "gender" ) ,
            @Result( column = "birthdate" , jdbcType = JdbcType.DATE, property = "birthdate" ) ,
            @Result( column = "married" , property = "maritalStatus" )
    })
    @Select( "SELECT id , name , gender , birthdate , married " +
             "FROM t_persons " +
             "ORDER BY id" )
    List<Person> findAll();

}
